<?php

namespace App\Admin\Controllers;

use App\Models\Banner;
use App\Models\Store;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BannerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '轮播图';
    protected $status = [1 => '正常', 2 => '禁用'];
    protected $mold = [
        'page' => '小程序页面',
        'unlink' => '不跳转',
        'program' => '其他小程序',
        'web' => 'H5页面'
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banner());
        //dd(Admin::user()->permissions);
	    //dd(Admin::user()->roles);
	    $grid->model()->where('store_id', 0);
        $grid->column('title', __('Title'));
        $grid->column('images_url', __('Images url'))->image('', 60);
        $grid->column('mold', __('Mold'))->using($this->mold);
        $grid->column('link', __('Link'));
        $grid->column('appid', __('Appid'));
        $grid->column('sort', __('Sort'))->sortable();
        $grid->column('status', __('Status'))
            ->using([1 => '正常', 2 => '禁用'])
            ->dot([1 => 'success', 2 => 'danger']);
        $grid->column('created_at', __('Created at'))->sortable();
        
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->like('title', __('Title'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Banner::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('images_url', __('Images url'));
        $show->field('mold', __('Mold'));
        $show->field('link', __('Link'));
        $show->field('appid', __('Appid'));
        $show->field('store_id', __('Store id'));
        $show->field('sort', __('Sort'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banner());

        $form->hidden('store_id', __('Store id'))
            ->default(Admin::user()->store_id);
        
        $form->text('title', __('Title'))->required();
        
        $form->image('images_url', '封面图片(720*375)')
            ->rules('required');
        
        $form->radio('mold', __('Mold'))
            ->options(['page' => '小程序页面', 'unlink' => '不跳转', 'program' => '其他小程序', 'web' => 'H5页面'])
            ->when('program', function (Form $form) {
                $form->hidden('link', '链接');
                $form->text('appid', __('Appid'));
            })
            ->when('page', function (Form $form) {
                $form->hidden('appid');
                $form->text('link', '小程序页面');
            })
            ->when('web', function (Form $form) {
                $form->hidden('appid');
                $form->text('link', '链接');
            })
            ->when('unlink', function (Form $form) {
                $form->hidden('appid')->default('');
                $form->hidden('link')->default('');
            })
            ->required();
        
        $form->number('sort', __('Sort'))->default(50);
        
        $form->radio('status', __('Status'))->options($this->status)->default(1);
    
        return $form;
    }
}
